**********************************************************************
*Patient level regs - Elevance quality 
*II. ANALYSIS	
**********************************************************************
	*File options 
	if $option ==1 {
	local is is
	}
	if $option ==2 {
	local is ss
	}
	
	use "$reg\cardiosample`is'", clear
	
	*CONTROL VARS AND WEIGHTS
	{
	local plan plan_prod1 plan_prod2 plan_prod3 plan_prod4 plan_rel1 plan_rel2 plan_rel3 indiv_aca indiv_offexchange_flg fullyinsured_flg 
	local pc female age elix_ inp_util_c 
	local hc2 bdtot teach mcare mcaid type_gac 	
	local area rural white college unempl poverty elderly state_exp_status 		
	local hospcontrol `hc2' `area' hhi
	local ptntcontrol `pc' `plan'
	local drg tot_drgwt
	local control never 
	local treat (incl_1`is')	
	local fe i.year i.num_prv
	local print keep(bought_1`is')
	
	*Use matching weights throughout 
	local WT "[aw=wt_cemm]"
	}
	
	*1. Elevance quality 
	*Tables 6, A9
	capture rm "$op\pregs_cardio_inp`is'.xls"
	capture rm "$op\pdvmeancardio_inp`is'.xls"
	foreach var in p_read90_inp { 
	
	*Preferred DD 
	reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' `drg' if (`treat' | `control') `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp`is'.xls", append `print' nocons ctitle("`is'_DW_`var'") 
	
	*Preferred event study 
	qui reg `var' pre3_1`is' pre2_1`is' pre0_1`is' post1_1`is' post2_1`is' post3_1`is'2 `hospcontrol' `ptntcontrol' `drg' `fe' if (`treat' | `control') `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp`is'.xls", append keep(pre* post*) nocons ctitle("`is'_DW_`var'") 
	
	*DV mean & pre-trend test 
	test pre3_1`is' pre2_1`is' 
	gen pre = round(r(p),.01)
	qui tabout pre1_1`is' `WT' if (`treat' | `control') using "$op\pdvmeancardio_inp`is'.xls", sum cells(mean `var' N `var' mean pre) f(4 0 2) total("`var'_`is'") append	
	drop pre 
	}
	
	*2. Readmissions by duration
	*Figure A5a
	foreach i in 30 45 60 75 90 105 {
	local var p_read`i'_inp 	
	
	reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' `drg' if (`treat' | `control') `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp`is'.xls", append `print' nocons ctitle("`is'_DW_`var'") 		
	
	qui tabout pre1_1`is' `WT' if (`treat' | `control') using "$op\pdvmeancardio_inp`is'.xls", sum cells(mean `var' N `var') f(4 0) total("`var'_`is'") append	
	}
	
	*3. Patient mix 
	*Tables A3, A7
	{
	preserve 
	drop if missing(p_read90_inp)
	foreach var in tot_drgwt age len elix_ {	
	qui sum `var' `WT', d
	replace `var' = r(mean) if missing(`var')
	}
	
	foreach var in tot_drgwt age len elix_ {	
	reg `var' bought_1`is' `fe'           if (`treat' | `control') `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp`is'.xls", append `print' nocons ctitle("ptnt_`var'") 	

	qui reg `var' bought_1`is' `fe' `drg' if (`treat' | `control') `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp`is'.xls", append `print' nocons ctitle("ptnt_D_`var'") 	
	
	qui tabout pre1_1`is' if (`treat' | `control') `WT' using "$op\pdvmeancardio_inp`is'.xls", sum cells(mean `var' N `var') f(4 0) total("`var'_`is'") append	
	}
	restore 
	}
	
	*4. Robustness I 
	*Table 7, A10
	if "`is'"=="is" {
	capture rm "$op\pregs_cardio_inp2`is'.xls"
	foreach var in p_read90_inp { 
	
	*No controls 
	reg `var' bought_1`is'   `fe' if (`treat' | `control') `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp2`is'.xls", append `print' nocons ctitle("`is'_noc_`var'") 

	*No DRG weights
	qui reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol'       if (`treat' | `control') `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp2`is'.xls", append `print' nocons ctitle("`is'_c_`var'") 
	
	*DRG "fixed effects"  
	qui reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' i.drg if (`treat' | `control') `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp2`is'.xls", append `print' nocons ctitle("`is'_DRG_`var'") 	

	*Narrower market definition (HHI using HSA) 
	ren hhi hhi_
	ren hhi_hsa hhi
	qui reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' `drg' if (`treat' | `control') `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp2`is'.xls", append `print' nocons ctitle("`is'_DWhhihsa_	`var'") 
	ren hhi hhi_hsa 
	ren hhi_ hhi 
	
	*No matching
	qui reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' `drg' if (`treat' | `control'), vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp2`is'.xls", append `print' nocons ctitle("`is'_DWnomatch_`var'") 
	}
	}
	
	*5. Robustness II 
	*Table 7, A10  
	if "`is'"=="is" {
	capture rm "$op\pregs_cardio_inp3`is'.xls"
	foreach var in p_read90_inp {
	
	*Non-neighbor control group 
	qui reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' `drg' if (`treat' | `control') & within!=1 `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp3`is'.xls", append `print' nocons ctitle("`is'_DW_`var'_exclwithin") 
	
	*Cluster by deal, HRR
	qui reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' `drg' if (`treat' | `control') `WT', vce(cluster deal_cluster) 
	outreg2 using "$op\pregs_cardio_inp3`is'.xls", append `print' nocons ctitle("`is'_DW_clustdeal_`var'") 
	
	qui reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' `drg' if (`treat' | `control') `WT', vce(cluster hrrcode) 
	outreg2 using "$op\pregs_cardio_inp3`is'.xls", append `print' nocons ctitle("`is'_DW_clusthrr_`var'") 
	
	*Matching on profits 
	ren wt_cemm_s main_wt_cemm_s 
	capture noisily merge m:1 num ye using "$reg\fulldata_wts`is'_profits"
	capture drop if _m==2
	capture drop _m
	replace wt_cemm = 0 if missing(wt_cemm)
	
	qui reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' `drg' if (`treat' | `control') `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp3`is'.xls", append `print' nocons ctitle("`is'_DW_`var'_profitmatch") 
	drop wt_cemm_s
	ren main_wt_cemm_s wt_cemm_s 

	*Balanced treatment panel
	qui reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' `drg' if (`treat' | `control') & bal_22_only==1 `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp3`is'.xls", append `print' nocons ctitle("`is'_DW_`var'_bal22only") 
	
	*Exclude deal year 
	qui reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' `drg' if (`treat' | `control') & pre0_1`is'!=1 `WT', vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp3`is'.xls", append `print' nocons ctitle("`is'_DW_`var'_dropye0") 

	*Treated soon control group (unmatched)
	if "`is'"=="is" {
	qui reg `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' `drg' if (`treat' | never_next_3yr), vce(cluster num_prv) 
	outreg2 using "$op\pregs_cardio_inp3`is'.xls", append `print' nocons ctitle("`is'_LRD_`var'_treatsoon") 
	}
	
	*Logit 
	logit `var' bought_1`is'   `fe' `ptntcontrol' `hospcontrol' `drg' if (`treat' | `control') [pw=wt_cemm], vce(cluster num_prv) 
	margins, dydx(bought_1`is') post
	outreg2 using "$op\pregs_cardio_inp3`is'.xls", append `print' nocons ctitle("`is'_Logit_`var'") dec(4) 	
	}
	}